In software development, logs serve as a record of events within an application, providing crucial insights for debugging. When logging, it is
essential to ensure that the logs are:
- easily accessible
- uniformly formatted for readability
- properly recorded
- securely logged when dealing with sensitive data
Those requirements are not met if a program directly writes to the standard outputs (e.g., print). That is why defining and using a dedicated
logger is highly recommended.
If you are using Flutter, you can use debugPrint
or surround print calls with a check for kDebugMode
.
Code examples
Noncompliant code example
void doSomething(int x) {
// ...
print('debug: $x');
// ...
}
Compliant solution
void doSomething(int x) {
// ...
debugPrint('debug: $x');
// ...
}
or
void doSomething(int x) {
// ...
if (kDebugMode) {
print('debug: $x');
}
// ...
}
or
void doSomething(int x) {
// ...
log('log: $x');
// ...
}